﻿Public Class frmDocGia

    Private Sub datDocGia_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datDocGia.CellContentClick

    End Sub

    Private Sub frmDocGia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetCbBox(cboDTUT, "TenDTUT", "MaDTUT", clsUuTien.DSUT)
        SetCbBox(cboPhanLoai, "TenDTUT", "MaDTUT", clsUuTien.DSUT)
        datDocGia.DataSource = clsDocGia.DSDocGia

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        PhucHoiNutSua()
        clsDocGia.Them(txtMaDG.Text, txtTenDG.Text, dtNgaySinh.Value, cboGioiTinh.Text, _
                       txtDiaChi.Text, dtNgayTao.Value, dtNgayHetHan.Value, cboDTUT.SelectedValue, txtCMDN.Text)
        datDocGia.DataSource = clsDocGia.DSDocGia
        btnLamLai_Click(sender, e)
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        PhucHoiNutSua()
        datDocGia.DataSource = clsDocGia.TimKiem(txtTimKiem.Text)
    End Sub

    Private Sub btnReLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReLoad.Click
        datDocGia.DataSource = clsDocGia.DSDocGia
        txtTimKiem.Text = ""

    End Sub
    Private Sub PhucHoiNutSua()
        If btnSua.Text = "Lưu lại" Then
            btnSua.Text = "Sửa"
        End If
    End Sub

    Private Sub btnLamLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamLai.Click
        txtMaDG.Text = ""
        txtTenDG.Text = ""
        txtDiaChi.Text = ""
        txtCMDN.Text = ""
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "Sửa" Then
            txtMaDG.Text = datDocGia.CurrentRow.Cells("MaDG").Value
            txtTenDG.Text = datDocGia.CurrentRow.Cells("TenDG").Value
            txtDiaChi.Text = datDocGia.CurrentRow.Cells("DiaChi").Value
            txtCMDN.Text = datDocGia.CurrentRow.Cells("CMND").Value
            dtNgaySinh.Value = datDocGia.CurrentRow.Cells("NgaySinh").Value
            dtNgayTao.Value = datDocGia.CurrentRow.Cells("NgayTao").Value
            dtNgayHetHan.Value = datDocGia.CurrentRow.Cells("NgayHetHan").Value
            cboDTUT.Text = datDocGia.CurrentRow.Cells("TenDTUT").Value
            cboGioiTinh.Text = datDocGia.CurrentRow.Cells("GioiTinh").Value
            btnSua.Text = "Lưu lại"
        Else
            btnSua.Text = "Sửa"

            clsDocGia.Sua(txtMaDG.Text, txtTenDG.Text, dtNgaySinh.Value, cboGioiTinh.Text, txtDiaChi.Text, dtNgayTao.Value, dtNgayHetHan.Value, cboDTUT.SelectedValue, txtCMDN.Text)
            datDocGia.DataSource = clsDocGia.DSDocGia
            btnLamLai_Click(sender, e)
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        PhucHoiNutSua()

        If MsgBox("Bạn có muốn xóa độc giả có mã là " & txtMaDG.Text & " không ?", MsgBoxStyle.YesNo, "Thông báo") = DialogResult.Yes Then
            

            If clsDocGia.Xoa(txtMaDG.Text) = False Then
                MsgBox("Không thể xóa độc giả có mã " & txtMaDG.Text & " vì chưa xóa các bản ghi liên quan.", MsgBoxStyle.Critical, "Lỗi")
            End If
            datDocGia.DataSource = clsDocGia.DSDocGia
        End If
    End Sub

    Private Sub datDocGia_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datDocGia.CellDoubleClick
        PhucHoiNutSua()
        btnSua_Click(sender, e)

    End Sub

    Private Sub btnPhanLoai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPhanLoai.Click
        datDocGia.DataSource = clsDocGia.PhanLoaiDG(cboPhanLoai.SelectedValue) 'cay nay la seletected value vi e tim theo ma~ ma`

    End Sub
End Class